/*******************************************************************************
 * Licensed to the OKChem
 *
 * http://www.okchem.com
 *
 *******************************************************************************/
package com.lz.service;

import com.lz.dao.OrderDao;
import com.lz.dao.ProductDao;
import com.lz.entity.ProductOrder;
import com.lz.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

/**
 * Created by toy lv on 2018/1/10 0010.
 *
 * @author:toy lv
 * @DATE:2018/1/10 0010
 */
@Service
public class ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private OrderDao orderDao;

    @Transactional
    public synchronized void testUpdate(){
        Product product=productDao.findOne("aaaa");
        if(product.getStock() ==0){
            throw new RuntimeException();
        }
        ProductOrder order=new ProductOrder();
        order.setProductId(product.getId());
        Random r=new java.util.Random();
        order.setUserName(r.nextInt()+"");
        orderDao.save(order);
        product.setStock(product.getStock()-1);
        productDao.save(product);


    }
}
